﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUL;
using Entity;

namespace PM
{
    public partial class DetailView1 : System.Web.UI.Page
    {
        private string _newsId = "";
        private string _grnId = "";
        private readonly NewsBul _news = new NewsBul();
        protected void Page_Load(object sender, EventArgs e)
        {
            _newsId = Convert.ToString(Page.RouteData.Values["newsid"]);
            _grnId = Convert.ToString(Page.RouteData.Values["grnid"]);
            LoadContent(_newsId);
            LoadPost(_newsId);
            Subnav(_grnId);
            Related(_newsId);
        }

        private void LoadContent(string newid)
        {
            var list = _news.News_GetById(newid);
            var strMenu = "";
            foreach (var items in list)
            {
                strMenu = strMenu + string.Format(
                              "<h2>{0}</h2>" +
                              "<h6>{1}</h6>" +
                              "<p>{2}</p>",
                              items.Name, items.Content,Common.DateTimeClass.ConvertDateTime(items.Date,"dd/MM"));
            }

            ltrView.Text = strMenu;
        }
        private void LoadPost(string newspostid)
        {
            var list = _news.News_GetByTop("5", "Id!=" + newspostid + "", "Date desc");
            var strMenu = "<ul>";
            foreach (var items in list)
            {
                string strLink = "/News/" + items.GroupNewsId + "/" + items.Id + "/" + Common.StringClass.NameToTag(items.Name);
                strMenu = strMenu + string.Format("<li><a href=\"{0}\">{1}</a></li>",strLink, items.Name);
            }
            strMenu += "</ul>";
            ltrPost.Text = strMenu;
        }
        private void Related(string newsrelate)
        {
            var list1 = _news.News_GetByTop("5", "Id!=" + newsrelate + "", "Date desc");
            var strMenu = "<ul>";
            foreach (var items1 in list1)
            {
                var list2 = _news.News_GetByTop("10", "Id!=" + items1.Id + "", "Date desc");
                foreach (var items2 in list2)
                {
                    string strLink = "/News/" + items2.GroupNewsId + "/" + items2.Id + "/" +
                                     Common.StringClass.NameToTag(items2.Name);
                    strMenu = strMenu + string.Format("<li><a href=\"{0}\">{1}</a></li>", strLink, items2.Name);
                }
            }
            strMenu += "</ul>";
            ltrRel.Text = strMenu;
        }
        private void Subnav(string gri)
        {
            var list = _news.News_GetByTop("10", "GroupNewsId="+gri+"", "Date desc");
            var strMenu = "<ul>";
            foreach (var items in list)
            {
                string strLink = "/News/" + items.GroupNewsId + "/" + items.Id + "/" + Common.StringClass.NameToTag(items.Name);
                strMenu = strMenu + string.Format("<li><a href=\"{0}\">{1}</a></li>", strLink, items.Name);
            }
            strMenu += "</ul>";
            ltrSub.Text = strMenu;
        }
    }
}